package com.cci.kangdao.dao;

import com.cci.kangdao.entity.FileEntity;
import org.apache.ibatis.annotations.*;
import org.mybatis.spring.annotation.MapperScan;

import java.util.List;
import java.util.Map;

@MapperScan
public interface CertificateDao {

	/**
	 * 获取服务站技师列表
	 * @param ServiceStationID
	 * @return
	 */
	@Select("select a.ID as userId, a.Contact as contact, a.Phone as phone from User_T a\n" +
			"left join UserHasRoles_T b on a.ID=b.UserID\n" +
			"left join UserGroupHasRole_T c on c.ID = b.RolesID\n" +
			"left join Roles_T d on c.RoleId = d.ID\n" +
			"where d.RolesNo='R0033' and a.Flag = 0 and b.Flag = 0\n" +
			"and c.Flag = 0 and d.Flag = 0 and a.ServiceStationID = #{ServiceStationID}")
	List<Map<String, Object>> getTechnicianList(@Param("ServiceStationID") Long ServiceStationID);

	/**
	 * 获取技师认证信息(之前下面的sql返回的是集合   会报错    我这做了变动按照时间倒叙取第一条)
	 * @param userId
	 * @return
	 */
	@Select("select top 1 u.ID as userId, u.Contact as contact, u.Phone as phone, c.ID as certId, c.ProgramId as programId " +
			",c.WorkStatus as workStatus,c.StartTimeOff as startTimeOff,c.EndTimeOff as endTimeOff "
			+ "from User_T u left join TechnicianCertificate_T c on u.ID = c.UserId where c.Flag = 0 and u.ID = #{userId} order by c.CreateTime desc")
	Map<String, Object> getTechnicianCert(@Param("userId") Long userId);

	/**
	 * 获取技师认证照片
	 * @param certificateId
	 * @return
	 */
	@Select("select p.ID as picId, p.PictureLink as picLink from TechnicianCertificatePicture_T p "
			+ "where Flag = 0 and p.CertificateId = #{certificateId}")
	List<Map<String, Object>> getTechnicianCertPics(@Param("certificateId") Long certificateId);

	/**
	 * 验证当前用户是否有认证课程记录
	 * @param userId
	 * @return
	 */
	@Select("select COUNT(1) from TechnicianCertificate_T where UserId = #{userId} and Flag = 0")
	int getTechnicianCertCountByUserId(@Param("userId") Long userId);

	/**
	 * 插入认证课程记录
	 * @param map
	 * @return
	 */
	@Insert("insert into TechnicianCertificate_T (UserId, ProgramId, Creator, CreateTime,WorkStatus,StartTimeOff,EndTimeOff) values "
			+ "(#{userId}, #{programId}, #{creatorId}, #{createTime},#{workStatus},#{startTimeOff},#{endTimeOff})")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	int addTechnicianCert(Map<String, Object> map);


    void addTechnicianCertPics(@Param("certificateId") Long certificateId, @Param("fileList") List<FileEntity> fileList
            , @Param("userId") Long userId, @Param("currentTime") String currentTime);

    @Update("update TechnicianCertificate_T set ProgramId = #{programId}, Updator = #{userId}, UpdateTime = #{currentTime}," +
            "WorkStatus=#{workStatus},StartTimeOff =#{startTimeOff},EndTimeOff=#{endTimeOff} where ID = #{certificateId}")
    int updateTechnicianCert(@Param("certificateId") Long certificateId, @Param("programId") String programId
            , @Param("userId") Long userId, @Param("currentTime") String currentTime, @Param("workStatus") String workStatus
            , @Param("startTimeOff") String startTimeOff, @Param("endTimeOff") String endTimeOff);


    @Update("<script>update TechnicianCertificatePicture_T set Flag = -1, Updator = #{userId}, UpdateTime = #{currentTime} where ID in <foreach collection='delPics' item='item' open='(' separator=',' close=')'>#{item} </foreach> </script>")
    int updateTechnicianCertPics(@Param("delPics") List<String> delPics, @Param("userId") Long userId, @Param("currentTime") String currentTime);


    @Insert("insert into TechnicianCertificate_T (UserId,  Creator, CreateTime,WorkStatus,StartTimeOff,EndTimeOff) values "
            + "(#{userId},#{creatorId}, #{createTime},#{workStatus},#{startTimeOff},#{endTimeOff})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int addTechnicianCertByUserId(Map<String, Object> map);

    @Update("update TechnicianCertificate_T set  Updator = #{updator}, UpdateTime = #{updateTime}," +
            "WorkStatus=#{workStatus},StartTimeOff =#{startTimeOff},EndTimeOff=#{endTimeOff} where ID = #{certId}")
    int updateTechnicianCertById(Map<String, Object> map);

	/**
	 * 查询技师认证数据 (之前下面的sql返回的是集合   会报错    我这做了变动按照时间倒叙取第一条)
	 * @param userId
	 * @return
	 */
	@Select("select top 1 tc.id ,tc.UserId as userId,tc.ProgramId as programId,tc.Creator as creatorId,tc.CreateTime as createTime " +
			"  ,tc.WorkStatus as workStatus,tc.StartTimeOff as startTimeOff,tc.EndTimeOff as endTimeOff from TechnicianCertificate_T as tc " +
			" inner join User_T UT on tc.UserId = UT.ID " +
			" where tc.UserId = #{userId} and UT.Flag = 0 and tc.Flag = 0 order by tc.CreateTime desc")
	Map<String, Object> getTechnicianAllByUserId(@Param("userId") Long userId);



}
